import deptPageApi from '@/api/dept/deptPageApi'
import { listToTree } from '@/util/CommonUtil'
import { TreeNodeData } from '@douyinfe/semi-ui/lib/es/tree'
import { useEffect, useState } from 'react'

// 获取所有部门列表
export default function useEffectDeptPage(noLoading: boolean) {
  const [deptList, setDeptList] = useState<TreeNodeData[]>([])

  useEffect(() => {
    if (!noLoading) {
      deptPageApi({
        pageSize: -1,
        defaultOrderFlag: true,
        orderList: [{ name: 'orderNo', value: 'descend' }],
      }).then(({ data }) => {
        setDeptList(
          listToTree(
            data.data.records.map((item) => ({
              id: item.id,
              key: item.id + '',
              value: item.id,
              label: item.name,
              parentId: item.parentId + '',
            })) as TreeNodeData[]
          )
        )
      })
    }
  }, [noLoading])

  return { deptList }
}
